package com.example.springmvcdemo;

import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.apache.logging.log4j.message.StringFormattedMessage;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.List;

@RequestMapping("/request")
@RestController
public class RequestController {
    @RequestMapping("/param1")
    public String param1(String userName){
        System.out.println("receive username: "+ userName);
        return "receive username: "+userName;
    }

    @RequestMapping("/param2")
    public String param2(Integer age){
        System.out.println("receive age:" + age);
        return "receive age: "+age;
    }

    @RequestMapping("/param3")
    public String param3(int age){
        System.out.println("receive age: "+ age);
        return "receive age: "+age;
    }

    @RequestMapping("/param4")
    public String param4(String userName,String password){
        System.out.println("receive userName:"+userName+" password:"+ password);
        return "receive userName:"+userName+" password:"+ password;
    }

    @RequestMapping("/param5")
    public String param5(UserInfo userInfo){
        System.out.println("receive userInfo: "+userInfo);
        return "receive userInfo: "+userInfo;
    }

    @RequestMapping("/param6")
    public String param6(@RequestParam(value = "userName",required = false)String name,String password){
        System.out.println("receive userName:"+name+" password:"+ password);
        return "receive name: "+name+" password:"+ password;
    }

    @RequestMapping("/param7")
    public String param7(String[] arrParam){
        System.out.println("receive arrParam: "+arrParam);
        return "receive arrParam: "+ Arrays.toString(arrParam);
    }

    @RequestMapping("/param8")
    public String param8(@RequestParam("listParam") List<String> listParam){
        System.out.println("receive listParam: "+listParam);
        return "receive listParam: "+listParam;
    }

    @RequestMapping("/param9")
    public String param9(@RequestBody UserInfo userInfo){
        System.out.println("receive userInfo: "+userInfo);
        return "receive userInfo: "+userInfo;
    }

    @RequestMapping("/param10/{articlName}")
    public String param10(@PathVariable("articlName") Integer articlName){
        System.out.println("receive articlName: "+articlName);
        return "receive articlName: "+articlName;
    }

    @RequestMapping("/param11/{name}/{id}")
    public String param11(@PathVariable("name") String name,@PathVariable("id") Integer id){
        System.out.println("receive name: "+name+" id: "+id);
        return "receive name: "+name+" id: "+id;
    }

    @RequestMapping("/param12")
    public String param12(@RequestParam("file") @RequestPart MultipartFile file1) throws IOException {
        System.out.println(file1.getName());
        System.out.println(file1.getOriginalFilename());
        System.out.println(file1.getContentType());
        file1.transferTo(new File("D:/temp/"+file1.getOriginalFilename()));
        return file1.getOriginalFilename();
    }

    @RequestMapping("/getCookie")
    public String getCookie(HttpServletRequest request) {
        Cookie[] cookies = request.getCookies();
        for(Cookie cookie:cookies) {
            System.out.println(cookie.getName()+" "+cookie.getValue());
        }
        System.out.println("get cookies successfully");
        return "get cookies successfully";
    }

    @RequestMapping("/getCookie2")
    public String getCookie2(@CookieValue("lzq") String value) {
        System.out.println(value);
        System.out.println("get cookies successfully");
        return "get cookies successfully";
    }

    @RequestMapping("/setSession")
    public String setSession(HttpServletRequest request) {
        //true表示存在就直接返回session对象,否则创造一个session对象
        HttpSession session = request.getSession(true);
        session.setAttribute("name","zhangsan");
        session.setAttribute("age","22");
        System.out.println("set session successfully");
        return "set session successfully";
    }

    @RequestMapping("/getSession")
    public String getSession(HttpServletRequest request) {
        //false表示存在就直接返回session对象,否则返回null
        HttpSession session = request.getSession(false);
        System.out.println(session.getAttribute("name"));
        System.out.println(session.getAttribute("age"));
        System.out.println("get session successfully");
        return "get session successfully";
    }

    @RequestMapping("/getSession2")
    public String getSession2(HttpSession session) {
        System.out.println(session.getAttribute("name"));
        System.out.println(session.getAttribute("age"));
        System.out.println("get session successfully");
        return "get session successfully";
    }

    @RequestMapping("/getSession3")
    public String getSession3(@SessionAttribute(value = "name",required = false) String name) {
        System.out.println("name "+name);
        System.out.println("get session successfully");
        return "get session successfully";
    }

    @RequestMapping("/getHeader")
    public String getHeader(HttpServletRequest request) {
        String userAgent = request.getHeader("User-Agent");
        System.out.println(userAgent);
        System.out.println("get userAgent successfully");
        return "get userAgent successfully";
    }

    @RequestMapping("/getHeader2")
    public String getHeader2(@RequestHeader("User-Agent") String userAgent) {
        System.out.println(userAgent);
        System.out.println("get userAgent successfully");
        return "get userAgent successfully";
    }
}
